/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.page.menu.inbox;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Contact;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.facade.ContactFacade;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.contact.ContactForm;
import edu.mobbuzz.renderer.ContactRenderer;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;
import java.util.Vector;

/**
 *
 * @author irw
 */
public class RecepientContactPage extends Menu{
    
    private Form contactForm;
    private Container contactPanel;
    private Label menuTitle;
    private DefaultListModel contactListModel;
    private List contactlist;
    private Command selectCmd;
    private Command backCmd;
    private Command newCmd;
    private Message message;
    private Vector recepientArr;
    private ContactFacade contactFacade;
    private int mode;
    private int messageType;
    
    public RecepientContactPage(){

        contactForm = new Form();
        contactPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        contactListModel = new DefaultListModel(getContactFacade().displayContactList());

        selectCmd = new Command("Select"){
            public void actionPerformed(ActionEvent evt) {
                selectContact();
            }
        };

        backCmd = new Command("Back"){
            public void actionPerformed(ActionEvent evt) {
                backToRecepientPage();
            }
        };

        newCmd = new Command("New Contact"){
            public void actionPerformed(ActionEvent evt) {
                newContact();
            }
        };

    }

    public Vector getRecepientArr() {
        if(recepientArr==null){
            recepientArr = new Vector();
        }
        return recepientArr;
    }


    public void setRecepientArr(Vector recepientArr) {
        this.recepientArr = recepientArr;
    }

    public Message getMessage() {
        return message;
    }


    public void setMessage(Message message) {
        this.message = message;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public int getMessageType() {
        return messageType;
    }


    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }
    public void showPage(){
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);

        contactPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        contactPanel.addComponent(menuTitle);
        contactPanel.addComponent(createList(contactListModel, List.VERTICAL, new ContactRenderer()));

        contactForm.setLayout(new BorderLayout());
        contactForm.addComponent(BorderLayout.CENTER, contactPanel);        
        contactForm.addCommand(backCmd);
        if(contactListModel.getSize()==0)
            contactForm.addCommand(newCmd);
        else
            contactForm.addCommand(selectCmd);
        
        contactForm.setCommandListener(getActionlistener());
        contactForm.setScrollable(false);
        contactForm.show();
    }

    private List createList(DefaultListModel contactsListModel, int orientation, ListCellRenderer renderer) {
        contactlist = new List(contactsListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };
        contactlist.getStyle().setBgTransparency(0);
        contactlist.getStyle().setBorder(Border.getEmpty());
        contactlist.setListCellRenderer(renderer);
        contactlist.setOrientation(orientation);

        return contactlist;
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);

        if(game == Display.GAME_FIRE){
            selectContact();

        }else if(game == Display.GAME_DOWN){
            if(contactlist.getSelectedIndex()<(contactlist.size()-1)){
               contactlist.setSelectedIndex(contactlist.getSelectedIndex()+1);
            }else{
                contactlist.setSelectedIndex(0);
            }

        }else if(game == Display.GAME_UP){
             if(contactlist.getSelectedIndex()!=0){
                 contactlist.setSelectedIndex(contactlist.getSelectedIndex()-1);
             }else{
                 contactlist.setSelectedIndex(contactlist.size()-1);
             }

        }
    }

    

    public void backToRecepientPage(){
        RecepientPage contactPage = new RecepientPage();
        contactPage.setExitCmd(getExitCmd());
        contactPage.setActionlistener(getActionlistener());
        contactPage.setMessage(getMessage());
        contactPage.setMode(getMode());
        contactPage.setRecepientArr(getRecepientArr());
        contactPage.setMessageType(getMessageType());
        contactPage.showPage();
    }

    public void newContact() {
        ContactForm editContactForm = new ContactForm();
        editContactForm.setMode(ModeString.NEW);
        editContactForm.setRecepientMode(getMode());
        editContactForm.setMessage(getMessage());
        editContactForm.setExitCmd(getExitCmd());
        editContactForm.setMessageType(getMessageType());
        editContactForm.setActionlistener(getActionlistener());
        editContactForm.showPage();
    }
    
    public void selectContact() {
        if(!getRecepientArr().contains(((Contact)contactlist.getSelectedItem()).getName())){
            getRecepientArr().addElement(((Contact)contactlist.getSelectedItem()).getName());
        }
        
        RecepientListPage recepientListPage = new RecepientListPage();
        recepientListPage.setExitCmd(getExitCmd());
        recepientListPage.setActionlistener(getActionlistener());
        recepientListPage.setMessage(getMessage());
        recepientListPage.setMode(getMode());
        recepientListPage.setRecepientArr(getRecepientArr());
        recepientListPage.setMessageType(getMessageType());
        recepientListPage.showPage();
        
    }

    public ContactFacade getContactFacade() {
        if(contactFacade==null)
            contactFacade = new ContactFacade();
        return contactFacade;
    }
    
    public String getMenuTitle() {
        return MenuString.CONTACT;
    }

    

}
